/*
 * @Description: 接收
 * @Author: Zzt
 * @Date: 2022-11-11 14:52:39
 * @LastEditTime: 2023-01-18 10:07:02
 */
import VisualizationMain from '@/components/DataVisualization/VisualizationMain';
import Nav from '@/components/Nav';
import { Tabs } from 'antd';
import { CheckboxValueType } from 'antd/es/checkbox/Group';
import classNames from 'classnames/bind';
import { useEffect, useState } from 'react';
import { useReceiveContext } from '../contexts/ReceiveContext';
import styles from '../styles/receive.module.scss';
const { TabPane } = Tabs;
const cx = classNames.bind(styles);

const according = [
  { fondName: '按时间阶段', id: 0 },
  { fondName: '按档案类型', id: 1 },
  { fondName: '按移交单位', id: 2 },
  { fondName: '按填表人', id: 3 }
];
const segmentedList = [
  { label: '案卷总数', value: 'folderCounts' },
  { label: '文件总数', value: 'fileCounts' },
  { label: '电子文件数量', value: 'eFileCounts' },
  { label: '数据量', value: 'dataSize' },
  { label: '接收总次数', value: 'times' }
];
const table = [
  { title: '全宗', dataIndex: 'fondsName', key: 'fondsName' },
  { title: '档案类型', dataIndex: 'archiveTypeName', key: 'archiveTypeName' },
  { title: '案卷总数', dataIndex: 'folderCounts', key: 'folderCounts' },
  { title: '文件总数', dataIndex: 'fileCounts', key: 'fileCounts' },
  { title: '电子文件数量', dataIndex: 'eFileCounts', key: 'eFileCounts' },
  { title: '数据量（GB）', dataIndex: 'dataSize', key: 'dataSize' },
  { title: '接收总次数', dataIndex: 'times', key: 'times' }
];
const ReceiveWarp = () => {
  const { dataList, setTableData, setChartData, chartList } = useReceiveContext();
  const [limit, setLimit] = useState<any>({});
  const [segmented, setSegmented] = useState('folderCounts');
  const [fondValue, setFondValue] = useState({});

  // 选中的按照类型
  const [type, setType] = useState(0);
  const [options, setOptions] = useState([]);
  const [visibleData, setVisibleData] = useState([]);
  const [condition, setCondition] = useState(<></>);
  const [tableList, setTableList] = useState(table);
  const [xData, setXData] = useState([]);
  const [yData, setYData] = useState([]);
  const [listQuery, setListQuery] = useState({
    pageNo: 1,
    pageSize: 10,
    param: {}
  });
  /**
   * @description: 切换全宗
   * @param {number} val 选中的全宗
   */
  const handleAccordingChange = (val: any) => {
    console.log(val);
    setType(parseInt(val, 10));
  };

  const onChange = (checkedValues: CheckboxValueType[]) => {
    console.log('checked = ', checkedValues);
  };

  const onChangeNav = (value: any) => {
    setFondValue(value);
  };
  useEffect(() => {
    const param = { ...limit, type, ...fondValue };
    setChartData({ ...param });
    setListQuery((state) => {
      return { ...state, pageNo: 1 };
    });
  }, [limit, type, fondValue]);
  useEffect(() => {
    const param = { ...limit, type, ...fondValue };
    setTableData({ ...listQuery, param });
  }, [limit, type, fondValue, listQuery]);
  useEffect(() => {
    setVisibleData(dataList);
    setYData(chartList.map((item: any) => item.name));
    setXData(chartList.map((item: any) => item[segmented]));
  }, [dataList, chartList, segmented]);
  const ChangNo = (num: any) => {
    setListQuery((state) => {
      return { ...state, pageNo: num };
    });
  };
  useEffect(() => {
    const removed = table.slice(0);
    if (type === 0) {
      removed.splice(0, 0, { title: '年度', dataIndex: 'yearName', key: 'yearName' });
      removed.splice(
        3,
        0,
        { title: '交接方式', dataIndex: 'receiveModeName', key: 'receiveModeName' },
        { title: '移交单位', dataIndex: 'transferUnitName', key: 'transferUnitName' }
      );
      setTableList(removed);
      setOptions([
        {
          title: '交接方式',
          name: 'hondWay',
          checkList: [
            { value: 1, label: '在线' },
            { value: 2, label: '离线' }
          ]
        }
      ]);
      setCondition(<Nav onChangeNav={onChangeNav} />);
    }
    if (type === 1) {
      removed.splice(
        2,
        0,
        { title: '年度', dataIndex: 'year', key: 'fondsName' },
        { title: '移交单位', dataIndex: 'transferUnitName', key: 'fondsName' },
        { title: '交接方式', dataIndex: 'receiveModeName', key: 'receiveModeName' }
      );
      setTableList(removed);
      setOptions([
        {
          title: '交接方式',
          name: 'hondWay',
          checkList: [
            { value: 1, label: '在线' },
            { value: 2, label: '离线' }
          ]
        }
      ]);
      setCondition(<Nav onChangeNav={onChangeNav} />);
    }
    if (type === 2) {
      removed.splice(
        0,
        0,
        { title: '移交单位', dataIndex: 'transferUnitName', key: 'transferUnitName' },
        { title: '年度', dataIndex: 'yearName', key: 'yearName' }
      );
      setTableList(removed);
      setOptions([
        {
          title: '交接方式',
          name: 'hondWay',
          checkList: [
            { value: 1, label: '在线' },
            { value: 2, label: '离线' }
          ]
        }
      ]);
      setCondition(<Nav onChangeNav={onChangeNav} />);
    }
    if (type === 3) {
      removed.splice(
        0,
        0,
        { title: '填报人', dataIndex: 'registrantName', key: 'registrantName' },
        { title: '年度', dataIndex: 'yearName', key: 'yearName' }
      );
      setTableList(removed);
      setOptions([
        {
          title: '填报人',
          name: 'registrants',
          checkList: [
            { value: 1, label: '填报人1' },
            { value: 2, label: '填报人2' },
            { value: 3, label: '填报人3' },
            { value: 4, label: '填报人4' },
            { value: 5, label: '填报人5' }
          ]
        }
      ]);
      setCondition(<Nav onChangeNav={onChangeNav} />);
    }
  }, [type]);

  const onChangeYear = (value: any) => {
    setLimit(value.chioce);
    setSegmented(value.segmentValue);
  };
  return (
    <>
      <Tabs
        tabPosition="left"
        defaultActiveKey="0"
        className={cx('tab')}
        onTabClick={handleAccordingChange}
      >
        {according.map((item) => (
          <TabPane tab={<>{item.fondName}</>} key={item.id}>
            <VisualizationMain
              getList={setTableData}
              type={item.fondName}
              condition={condition}
              segmented={segmentedList}
              tableList={tableList}
              choiceList={options}
              data={{ visibleData, xData, yData }}
              onChange={onChangeYear}
              ChangNo={ChangNo}
            />
          </TabPane>
        ))}
      </Tabs>
    </>
  );
};
export default ReceiveWarp;
